<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Normal Test</title>
  </head>
  <body>
    <script src="/sw/register.js" type="module"></script>
    <script type="module">
      import { init } from "/packages/fs/main.js";
      import { test } from "/ok-test/main.js";

      // 初始化测试目录
      const testDirectory = await init("test-dir3");

      // 测试父目录是否相同
      await test("Parent Directory Comparison Test", async () => {
        const testFile = await testDirectory.get("a/b/c/d/test.txt", {
          create: "file",
        });
        const fileParentDir = await testFile.parent;

        const directoryD = await testDirectory.get("a/b/c/d");

        const isSame = await fileParentDir.isSame(directoryD);

        return {
          assert: isSame,
          content: `Expected file parent directory to be the same as directory 'd'`,
        };
      });

      // 测试根目录是否相同
      await test("Root Comparison Test", async () => {
        const testFile = await testDirectory.get("a/b/c/d/test.txt", {
          create: "file",
        });

        const root1 = testFile.root;

        const directoryD = await testDirectory.get("a/b/c/d");

        const root2 = directoryD.root;

        const isSame = await root1.isSame(root2);

        return {
          assert: isSame,
          content: `Expected root directory to be the same as directory 'd'`,
        };
      });

      // 测试删除
      await test("Size Test", async () => {
        const testFile = await testDirectory.get("a2/test2.txt", {
          create: "file",
        });

        await testFile.write("some text");

        const size = await testFile.size();

        return {
          assert: size === 9,
          content: `Expected file to be deleted`,
        };
      });

      await test("Dir Length Test", async () => {
        for (let i = 0; i < 10; i++) {
          const testFile = await testDirectory.get(`a3/test${i}.txt`, {
            create: "file",
          });

          await testFile.write("some text " + i);
        }

        const len = await (await testDirectory.get("a3")).length();

        return {
          assert: len === 10,
          content: `Expected length to be 10, got ${len}`,
        };
      });

      // 添加一个写入file的测试
      await test("Write File Test", async () => {
        const testFile = await testDirectory.get("test-write-file.txt", {
          create: "file",
        });

        const content1 = "some text" + Math.random();

        const file = new File([content1], "test-write-file.txt", {
          type: "text/plain",
        });
        await testFile.write(file);

        const content = await testFile.text();

        return {
          assert: content === content1,
          content: `Expected file content to be '${content1}', got '${content}'`,
        };
      });
    </script>
  </body>
</html>
